package com.mercator.client.map.presenter;

import org.vaadin.gwtgraphics.client.Shape;
import org.vaadin.gwtgraphics.client.shape.Circle;

import com.google.gwt.core.client.GWT;
import com.google.gwt.logging.client.ConsoleLogHandler;
import com.google.gwt.user.client.Window;
import com.mercator.client.graph.DCircleLayoutGraph;
import com.mercator.client.graph.DContainer;
import com.mercator.client.graph.DGeneSetMap;
import com.mercator.client.graph.DPhenomeMap;
import com.mercator.client.graph.GenStringNode;
import com.mercator.client.graph.GenType;
import com.mercator.client.graph.GeneConverter;
import com.mercator.client.graph.MapName;
import com.mercator.client.map.MapEventBus;
import com.mercator.client.map.view.MapView;
import com.mercator.client.util.JSOParser;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

@Presenter(view = MapView.class)
public class MapPresenter extends BasePresenter<MapView, MapEventBus> {

	private boolean first = true;
	
	public void onBeforeLoad() {
		bind();
		JSOParser jso = new JSOParser();
		DGeneSetMap dcg = new DGeneSetMap(view);
		jso.generateGraph(dcg);
		view.setMap(dcg);
		dcg.move();
		Window.alert("Child loaded");
		eventBus.changeMap(dcg.getContainer().getGeneSetsElems());
	}

	public void bind() {

		// JSOParser jso = new JSOParser();
		// DGeneSetMap dcg = new DGeneSetMap(view);
		// jso.generateGraph(dcg);
		// view.setMap(dcg);
	}

	public void onGoToMap(MapName name) {
		if(first){
			onBeforeLoad();
			first = false;}
		DContainer curMap = view.getMap();
	
			switch (name) {
			case GENESET_MAP:
				eventBus.changeMap(((DGeneSetMap)(curMap)).getContainer().getGeneSetsElems());
				if(curMap.getName() != MapName.GENESET_MAP){
				DGeneSetMap dMap = new DGeneSetMap(view);
				dMap.addAllNodes(curMap.getCol());
				view.setMap(dMap);}
				break;
			case CIRCLE_MAP:
				DCircleLayoutGraph<GenType, GenStringNode<? extends Shape>> circleMap = new DCircleLayoutGraph<GenType, GenStringNode<? extends Shape>>(
						view);
				circleMap.removeAll();
				circleMap.addAllNodes(curMap.getCol());
				view.setMap(circleMap);
				break;
			case PHENOME_MAP:
				view.clear();
				DPhenomeMap dpm = new DPhenomeMap(view);
				dpm.addAll(GeneConverter.convert(curMap.getCol()));
				view.setMap(dpm);
				break;
			}

		view.getMap().move();
		
	}

}
